( () => {
    "use strict";
    var e = {
        38666: e => {
            var t = Object.prototype.hasOwnProperty
              , n = "~";
            function s() {}
            function r(e, t, n) {
                this.fn = e,
                this.context = t,
                this.once = n || !1
            }
            function o(e, t, s, o, a) {
                if ("function" != typeof s)
                    throw new TypeError("The listener must be a function");
                var i = new r(s,o || e,a)
                  , c = n ? n + t : t;
                return e._events[c] ? e._events[c].fn ? e._events[c] = [e._events[c], i] : e._events[c].push(i) : (e._events[c] = i,
                e._eventsCount++),
                e
            }
            function a(e, t) {
                0 == --e._eventsCount ? e._events = new s : delete e._events[t]
            }
            function i() {
                this._events = new s,
                this._eventsCount = 0
            }
            Object.create && (s.prototype = Object.create(null),
            (new s).__proto__ || (n = !1)),
            i.prototype.eventNames = function() {
                var e, s, r = [];
                if (0 === this._eventsCount)
                    return r;
                for (s in e = this._events)
                    t.call(e, s) && r.push(n ? s.slice(1) : s);
                return Object.getOwnPropertySymbols ? r.concat(Object.getOwnPropertySymbols(e)) : r
            }
            ,
            i.prototype.listeners = function(e) {
                var t = n ? n + e : e
                  , s = this._events[t];
                if (!s)
                    return [];
                if (s.fn)
                    return [s.fn];
                for (var r = 0, o = s.length, a = new Array(o); r < o; r++)
                    a[r] = s[r].fn;
                return a
            }
            ,
            i.prototype.listenerCount = function(e) {
                var t = n ? n + e : e
                  , s = this._events[t];
                return s ? s.fn ? 1 : s.length : 0
            }
            ,
            i.prototype.emit = function(e, t, s, r, o, a) {
                var i = n ? n + e : e;
                if (!this._events[i])
                    return !1;
                var c, u, p = this._events[i], l = arguments.length;
                if (p.fn) {
                    switch (p.once && this.removeListener(e, p.fn, void 0, !0),
                    l) {
                    case 1:
                        return p.fn.call(p.context),
                        !0;
                    case 2:
                        return p.fn.call(p.context, t),
                        !0;
                    case 3:
                        return p.fn.call(p.context, t, s),
                        !0;
                    case 4:
                        return p.fn.call(p.context, t, s, r),
                        !0;
                    case 5:
                        return p.fn.call(p.context, t, s, r, o),
                        !0;
                    case 6:
                        return p.fn.call(p.context, t, s, r, o, a),
                        !0
                    }
                    for (u = 1,
                    c = new Array(l - 1); u < l; u++)
                        c[u - 1] = arguments[u];
                    p.fn.apply(p.context, c)
                } else {
                    var R, y = p.length;
                    for (u = 0; u < y; u++)
                        switch (p[u].once && this.removeListener(e, p[u].fn, void 0, !0),
                        l) {
                        case 1:
                            p[u].fn.call(p[u].context);
                            break;
                        case 2:
                            p[u].fn.call(p[u].context, t);
                            break;
                        case 3:
                            p[u].fn.call(p[u].context, t, s);
                            break;
                        case 4:
                            p[u].fn.call(p[u].context, t, s, r);
                            break;
                        default:
                            if (!c)
                                for (R = 1,
                                c = new Array(l - 1); R < l; R++)
                                    c[R - 1] = arguments[R];
                            p[u].fn.apply(p[u].context, c)
                        }
                }
                return !0
            }
            ,
            i.prototype.on = function(e, t, n) {
                return o(this, e, t, n, !1)
            }
            ,
            i.prototype.once = function(e, t, n) {
                return o(this, e, t, n, !0)
            }
            ,
            i.prototype.removeListener = function(e, t, s, r) {
                var o = n ? n + e : e;
                if (!this._events[o])
                    return this;
                if (!t)
                    return a(this, o),
                    this;
                var i = this._events[o];
                if (i.fn)
                    i.fn !== t || r && !i.once || s && i.context !== s || a(this, o);
                else {
                    for (var c = 0, u = [], p = i.length; c < p; c++)
                        (i[c].fn !== t || r && !i[c].once || s && i[c].context !== s) && u.push(i[c]);
                    u.length ? this._events[o] = 1 === u.length ? u[0] : u : a(this, o)
                }
                return this
            }
            ,
            i.prototype.removeAllListeners = function(e) {
                var t;
                return e ? (t = n ? n + e : e,
                this._events[t] && a(this, t)) : (this._events = new s,
                this._eventsCount = 0),
                this
            }
            ,
            i.prototype.off = i.prototype.removeListener,
            i.prototype.addListener = i.prototype.on,
            i.prefixed = n,
            i.EventEmitter = i,
            e.exports = i
        }
    }
      , t = {};
    function n(s) {
        var r = t[s];
        if (void 0 !== r)
            return r.exports;
        var o = t[s] = {
            exports: {}
        };
        return e[s](o, o.exports, n),
        o.exports
    }
    n.n = e => {
        var t = e && e.__esModule ? () => e.default : () => e;
        return n.d(t, {
            a: t
        }),
        t
    }
    ,
    n.d = (e, t) => {
        for (var s in t)
            n.o(t, s) && !n.o(e, s) && Object.defineProperty(e, s, {
                enumerable: !0,
                get: t[s]
            })
    }
    ,
    n.o = (e, t) => Object.prototype.hasOwnProperty.call(e, t);
    var s, r, o, a = n(38666), i = n.n(a);
    !function(e) {
        e.Installed = "Installed",
        e.NotDetected = "NotDetected",
        e.Loadable = "Loadable",
        e.Unsupported = "Unsupported"
    }(s || (s = {})),
    Error,
    function(e) {
        e.Testnet = "testnet3",
        e.TestnetBeta = "testnetbeta",
        e.MainnetBeta = "mainnetbeta"
    }(r || (r = {})),
    function(e) {
        e.NoDecrypt = "NO_DECRYPT",
        e.UponRequest = "DECRYPT_UPON_REQUEST",
        e.AutoDecrypt = "AUTO_DECRYPT",
        e.OnChainHistory = "ON_CHAIN_HISTORY"
    }(o || (o = {}));
    let c = (e=21) => crypto.getRandomValues(new Uint8Array(e)).reduce(( (e, t) => e + ((t &= 63) < 36 ? t.toString(36) : t < 62 ? (t - 26).toString(36).toUpperCase() : t > 62 ? "-" : "_")), "")
      , u = function(e) {
        return e.GetCurrentPermissionRequest = "GET_CURRENT_PERMISSION_REQUEST",
        e.GetCurrentPermissionResponse = "GET_CURRENT_PERMISSION_RESPONSE",
        e.PermissionRequest = "PERMISSION_REQUEST",
        e.PermissionResponse = "PERMISSION_RESPONSE",
        e.DisconnectRequest = "DISCONNECT_REQUEST",
        e.DisconnectResponse = "DISCONNECT_RESPONSE",
        e.OperationRequest = "OPERATION_REQUEST",
        e.OperationResponse = "OPERATION_RESPONSE",
        e.SignRequest = "SIGN_REQUEST",
        e.SignResponse = "SIGN_RESPONSE",
        e.BroadcastRequest = "BROADCAST_REQUEST",
        e.BroadcastResponse = "BROADCAST_RESPONSE",
        e.DecryptRequest = "DECRYPT_REQUEST",
        e.DecryptResponse = "DECRYPT_RESPONSE",
        e.RecordsRequest = "RECORDS_REQUEST",
        e.RecordsResponse = "RECORDS_RESPONSE",
        e.TransactionRequest = "TRANSACTION_REQUEST",
        e.TransactionResponse = "TRANSACTION_RESPONSE",
        e.ExecutionRequest = "EXECUTION_REQUEST",
        e.ExecutionResponse = "EXECUTION_RESPONSE",
        e.BulkTransactionsRequest = "BULK_TRANSACTIONS_REQUEST",
        e.BulkTransactionsResponse = "BULK_TRANSACTIONS_RESPONSE",
        e.DeployRequest = "DEPLOY_REQUEST",
        e.DeployResponse = "DEPLOY_RESPONSE",
        e.TransactionStatusRequest = "TRANSACTION_STATUS_REQUEST",
        e.TransactionStatusResponse = "TRANSACTION_STATUS_RESPONSE",
        e.GetExecutionRequest = "GET_EXECUTION_REQUEST",
        e.GetExecutionResponse = "GET_EXECUTION_RESPONSE",
        e.RecordPlaintextsRequest = "RECORD_PLAINTEXTS_REQUEST",
        e.RecordPlaintextsResponse = "RECORD_PLAINTEXTS_RESPONSE",
        e.TransactionHistoryRequest = "TRANSACTION_HISTORY_REQUEST",
        e.TransactionHistoryResponse = "TRANSACTION_HISTORY_RESPONSE",
        e
    }({})
      , p = function(e) {
        return e.NotGranted = "NOT_GRANTED",
        e.NotFound = "NOT_FOUND",
        e.InvalidParams = "INVALID_PARAMS",
        e.NetworkNotGranted = "NETWORK_NOT_GRANTED",
        e
    }({})
      , l = function(e) {
        return e.Request = "ALEO_PAGE_REQUEST",
        e.Response = "ALEO_PAGE_RESPONSE",
        e.ErrorResponse = "ALEO_PAGE_ERROR_RESPONSE",
        e
    }({});
    function R(e) {
        return new Promise(( (t, n) => {
            const s = c()
              , r = e => {
                const o = e.data;
                switch (!0) {
                case e.source !== window || (null == o ? void 0 : o.reqId) !== s:
                    return;
                case (null == o ? void 0 : o.type) === l.Response:
                    t(o.payload),
                    window.removeEventListener("message", r);
                    break;
                case (null == o ? void 0 : o.type) === l.ErrorResponse:
                    n(function(e) {
                        switch (console.log("Error: ", e),
                        !0) {
                        case e === p.NotGranted:
                            return new T;
                        case e === p.NotFound:
                            return new S;
                        case e === p.InvalidParams:
                            return new f;
                        default:
                            return new d
                        }
                    }(o.payload)),
                    window.removeEventListener("message", r)
                }
            }
            ;
            E({
                type: l.Request,
                payload: e,
                reqId: s
            }),
            window.addEventListener("message", r)
        }
        ))
    }
    function y(e) {
        if (!e)
            throw new Error("Invalid response recieved")
    }
    function E(e) {
        window.postMessage(e, "*")
    }
    class d {
        constructor() {
            this.name = "AleoWalletError",
            this.message = "An unknown error occured. Please try again or report it"
        }
    }
    class T extends d {
        constructor(...e) {
            super(...e),
            this.name = "NotGrantedAleoWalletError",
            this.message = "Permission Not Granted"
        }
    }
    class S extends d {
        constructor(...e) {
            super(...e),
            this.name = "NotFoundAleoWalletError",
            this.message = "Account Not Found. Try connect again"
        }
    }
    class f extends d {
        constructor(...e) {
            super(...e),
            this.name = "InvalidParamsAleoWalletError",
            this.message = "Some of the parameters you provided are invalid"
        }
    }
    class _ extends (i()) {
        constructor(...e) {
            super(...e),
            this.publicKey = void 0,
            this.permission = void 0,
            this.appName = void 0,
            this.network = void 0,
            this.clearAccountChangeInterval = void 0
        }
        async isAvailable() {
            return await new Promise((e => {
                const t = e => {
                    var t, s;
                    e.source === window && (null === (t = e.data) || void 0 === t ? void 0 : t.type) === l.Response && "PONG" === (null === (s = e.data) || void 0 === s ? void 0 : s.payload) && n(!0)
                }
                  , n = n => {
                    e(n),
                    window.removeEventListener("message", t),
                    clearTimeout(s)
                }
                ;
                E({
                    type: l.Request,
                    payload: "PING"
                }),
                window.addEventListener("message", t);
                const s = setTimeout(( () => n(!1)), 500)
            }
            ))
        }
        async signMessage(e) {
            const t = (new TextDecoder).decode(e)
              , n = await async function(e, t) {
                const n = await R({
                    type: u.SignRequest,
                    sourcePublicKey: e,
                    payload: t
                });
                return y(n.type === u.SignResponse),
                n.signature
            }(this.publicKey, t);
            return {
                signature: (new TextEncoder).encode(n)
            }
        }
        async decrypt(e, t, n, s, r) {
            const o = await async function(e, t, n, s, r, o) {
                const a = await R({
                    type: u.DecryptRequest,
                    sourcePublicKey: e,
                    cipherText: t,
                    tpk: n,
                    programId: s,
                    functionName: r,
                    index: o
                });
                return y(a.type === u.DecryptResponse),
                a.plainText
            }(this.publicKey, e, t, n, s, r);
            return {
                text: o
            }
        }
        async requestRecords(e) {
            const t = await async function(e, t) {
                const n = await R({
                    type: u.RecordsRequest,
                    sourcePublicKey: e,
                    program: t
                });
                return y(n.type === u.RecordsResponse),
                n.records
            }(this.publicKey, e);
            return {
                records: t
            }
        }
        async requestTransaction(e) {
            const t = await async function(e, t) {
                const n = await R({
                    type: u.TransactionRequest,
                    sourcePublicKey: e,
                    transaction: t
                });
                return y(n.type === u.TransactionResponse),
                n.transactionId
            }(this.publicKey, e);
            return {
                transactionId: t
            }
        }
        async requestExecution(e) {
            const t = await async function(e, t) {
                const n = await R({
                    type: u.ExecutionRequest,
                    sourcePublicKey: e,
                    transaction: t
                });
                return y(n.type === u.ExecutionResponse),
                n.transactionId
            }(this.publicKey, e);
            return {
                transactionId: t
            }
        }
        async requestBulkTransactions(e) {
            return {
                transactionIds: await async function(e, t) {
                    const n = await R({
                        type: u.BulkTransactionsRequest,
                        sourcePublicKey: e,
                        transactions: t
                    });
                    return y(n.type === u.BulkTransactionsResponse),
                    n.transactionIds
                }(this.publicKey, e)
            }
        }
        async requestDeploy(e) {
            const t = await async function(e, t) {
                const n = await R({
                    type: u.DeployRequest,
                    sourcePublicKey: e,
                    deployment: t
                });
                return y(n.type === u.DeployResponse),
                n.transactionId
            }(this.publicKey, e);
            return {
                transactionId: t
            }
        }
        async transactionStatus(e) {
            const t = await async function(e, t) {
                const n = await R({
                    type: u.TransactionStatusRequest,
                    sourcePublicKey: e,
                    transactionId: t
                });
                return y(n.type === u.TransactionStatusResponse),
                n.status
            }(this.publicKey, e);
            return {
                status: t
            }
        }
        async getExecution(e) {
            const t = await async function(e, t) {
                const n = await R({
                    type: u.GetExecutionRequest,
                    sourcePublicKey: e,
                    transactionId: t
                });
                return y(n.type === u.GetExecutionResponse),
                n.execution
            }(this.publicKey, e);
            return {
                execution: t
            }
        }
        async requestRecordPlaintexts(e) {
            const t = await async function(e, t) {
                const n = await R({
                    type: u.RecordPlaintextsRequest,
                    sourcePublicKey: e,
                    program: t
                });
                return y(n.type === u.RecordPlaintextsResponse),
                n.records
            }(this.publicKey, e);
            return {
                records: t
            }
        }
        async requestTransactionHistory(e) {
            const t = await async function(e, t) {
                const n = await R({
                    type: u.TransactionHistoryRequest,
                    sourcePublicKey: e,
                    program: t
                });
                return y(n.type === u.TransactionHistoryResponse),
                n.transactions
            }(this.publicKey, e);
            return {
                transactions: t
            }
        }
        async connect(e, t, n) {
            const s = await async function(e, t, n, s, r) {
                const o = await R({
                    type: u.PermissionRequest,
                    appMeta: e,
                    force: !1,
                    decryptPermission: n,
                    network: s,
                    programs: r
                });
                return y(o.type === u.PermissionResponse),
                {
                    rpc: o.network,
                    publicKey: o.publicKey,
                    decryptPermission: o.decryptPermission,
                    programs: o.programs
                }
            }({
                name: window.location.hostname
            }, 0, e, t, n);
            this.permission = s,
            this.publicKey = s.publicKey,
            this.network = t,
            this.clearAccountChangeInterval = function(e) {
                let t, n = null;
                const s = async () => {
                    try {
                        const t = await async function() {
                            const e = await R({
                                type: u.GetCurrentPermissionRequest
                            });
                            return y(e.type === u.GetCurrentPermissionResponse),
                            e.permission
                        }();
                        o = n,
                        (null === (r = t) ? null === o : r.publicKey === (null == o ? void 0 : o.publicKey) && r.rpc === (null == o ? void 0 : o.rpc)) || (e(t),
                        n = t)
                    } catch {}
                    var r, o;
                    t = setTimeout(s, 1e4)
                }
                ;
                return s(),
                () => clearTimeout(t)
            }((e => {
                this.emit("accountChange", e)
            }
            ))
        }
        async disconnect() {
            await async function() {
                const e = await R({
                    type: u.DisconnectRequest
                });
                return y(e.type === u.DisconnectResponse),
                e
            }(),
            this.publicKey = void 0,
            this.permission = void 0,
            this.clearAccountChangeInterval && this.clearAccountChangeInterval()
        }
    }
    const h = new _;
    try {
        Object.defineProperty(window, "leoWallet", {
            value: h,
            writable: !0
        })
    } catch (e) {
        console.error(e)
    }
}
)();
